package net.gxtvsgwt.client.demo.type;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.gxtvsgwt.client.demo.BaseDemo;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class GridDemo extends BaseDemo {

	@Override
	public Widget createGxt() {
		ListStore<BaseModelData> listStore = new ListStore<BaseModelData>();
		for (int i = 0; i < dataSize; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Col0", "test" + i + "0");
			map.put("Col1", "test" + i + "1");
			map.put("Col2", "test" + i + "2");
			listStore.add(new BaseModelData(map));
		}
		List<ColumnConfig> columnConfig = new ArrayList<ColumnConfig>();
		columnConfig.add(new ColumnConfig("Col0", "Col0", 100));
		columnConfig.add(new ColumnConfig("Col1", "Col1", 100));
		columnConfig.add(new ColumnConfig("Col2", "Col2", 100));
		ColumnModel columnModel = new ColumnModel(columnConfig);
		com.extjs.gxt.ui.client.widget.grid.Grid<BaseModelData> grid = new com.extjs.gxt.ui.client.widget.grid.Grid<BaseModelData>(
				listStore, columnModel);
		grid.setHeight("200px");
		grid.setWidth("300px");
		grid.setLazyRowRender(0);
		return grid;
	}

	@Override
	public Widget createGwt() {
		List<Map<String, Object>> listStore = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < dataSize; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Col0", "test" + i + "0");
			map.put("Col1", "test" + i + "1");
			map.put("Col2", "test" + i + "2");
			listStore.add(map);
		}
		CellTable<Map<String, Object>> grid = new CellTable<Map<String, Object>>();
		grid.addColumn(new Column<Map<String, Object>, String>(new TextCell()) {
			@Override
			public String getValue(Map<String, Object> object) {
				return (String) object.get("Col0");
			}
		}, "Col0");
		grid.addColumn(new Column<Map<String, Object>, String>(new TextCell()) {
			@Override
			public String getValue(Map<String, Object> object) {
				return (String) object.get("Col1");
			}
		}, "Col1");
		grid.addColumn(new Column<Map<String, Object>, String>(new TextCell()) {
			@Override
			public String getValue(Map<String, Object> object) {
				return (String) object.get("Col2");
			}
		}, "Col2");
		grid.setRowData(listStore);
		grid.setWidth("300px");
		SimplePanel panel = new SimplePanel();
		panel.add(grid);
		panel.setHeight("200px");
		panel.getElement().getStyle().setOverflow(Overflow.AUTO);
		return panel;
	}

	@Override
	public String getName() {
		return "Grid";
	}
}
